<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="订单号" v-bind="validateInfos.orderNo">
            <a-input v-model:value="formData.orderNo" placeholder="请输入订单号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保单号" v-bind="validateInfos.policyNo">
            <a-input v-model:value="formData.policyNo" placeholder="请输入保单号" disabled></a-input>
            <a :href="formData.policyDownloadUrl" target="_blank">下载保单</a>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="订单来源" v-bind="validateInfos.sourceType">
            <j-dict-select-tag v-model:value="formData.sourceType" dictCode="PPYWLX" placeholder="请输入订单来源" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险状态" v-bind="validateInfos.policyState">
            <j-dict-select-tag v-model:value="formData.policyState" dictCode="DZBDZT" placeholder="请输入保险状态" disabled />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="支付时间" v-bind="validateInfos.payTime">
		        <a-date-picker placeholder="请选择支付时间" v-model:value="formData.payTime" value-format="YYYY-MM-DD"  style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="付款人名称" v-bind="validateInfos.payAccountName">
            <a-input v-model:value="formData.payAccountName" placeholder="请输入付款人名称" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="出保时间" v-bind="validateInfos.insureActiveDate">
		        <a-date-picker placeholder="请选择出保时间" v-model:value="formData.insureActiveDate" value-format="YYYY-MM-DD"  style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保费" v-bind="validateInfos.cost">
	          <a-input-number v-model:value="formData.cost" placeholder="请输入保费" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保证金" v-bind="validateInfos.margin">
            <a-input-number v-model:value="formData.margin" placeholder="请输入保证金" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="渠道商" v-bind="validateInfos.channelProvider">
            <a-input v-model:value="formData.channelProvider" placeholder="请输入渠道商"  :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="地区" v-bind="validateInfos.area">
	          <j-dict-select-tag v-model:value="formData.area" dictCode="" placeholder="请选择地区" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="平台" v-bind="validateInfos.platformCode">
	          <j-dict-select-tag v-model:value="formData.platformCode" dictCode="" placeholder="请选择平台" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险公司" v-bind="validateInfos.insuranceCompany">
	          <j-dict-select-tag v-model:value="formData.insuranceCompany" dictCode="" placeholder="请选择保险公司" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="被保人" v-bind="validateInfos.insuredName">
            <a-input v-model:value="formData.insuredName" placeholder="请输入被保人" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="投保人名称" v-bind="validateInfos.policyHolderName">
            <a-input v-model:value="formData.policyHolderName" placeholder="请输入投保人名称" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="项目名称" v-bind="validateInfos.projectName">
            <a-input v-model:value="formData.projectName" placeholder="请输入项目名称" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="项目编号" v-bind="validateInfos.projectId">
            <a-input v-model:value="formData.projectId" placeholder="请输入项目编号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="邀请码" v-bind="validateInfos.inviteCode">
            <a-input v-model:value="formData.inviteCode" placeholder="请输入邀请码" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="业务流水号" v-bind="validateInfos.applyNo">
            <a-input v-model:value="formData.applyNo" placeholder="请输入业务流水号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="下单时间" v-bind="validateInfos.applyTime">
            <a-input v-model:value="formData.applyTime" placeholder="请输入下单时间" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="投保联系人名称" v-bind="validateInfos.policyLinkmanName">
            <a-input v-model:value="formData.policyLinkmanName" placeholder="请输入投保联系人名称" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="投保联系人手机号" v-bind="validateInfos.policyLinkmanMobilename">
            <a-input v-model:value="formData.policyLinkmanMobilename" placeholder="请输入投保联系人手机号" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="费率" v-bind="validateInfos.rate">
	          <a-input-number v-model:value="formData.rate" placeholder="请输入费率" style="width: 100%" disabled/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="投保人社会统一信用代码" v-bind="validateInfos.policyUcc">
            <a-input v-model:value="formData.policyUcc" placeholder="请输入投保人社会统一信用代码" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险天数" v-bind="validateInfos.policyDays">
            <a-input v-model:value="formData.policyDays" placeholder="请输入保险天数" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开标时间" v-bind="validateInfos.kaiBiaoTime">
            <a-input v-model:value="formData.kaiBiaoTime" placeholder="请输入开标时间" disabled></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../InsuranceStatistics.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    orderNo: '',   
    policyNo: '',   
    sourceType: '',   
    policyState: '',   
    payTime: '',   
    payAccountName: '',   
    insureActiveDate: '',   
    cost: undefined,
    channelProvider: '',   
    area: '',
    platformCode: '',
    insuranceCompany: '',   
    insuredName: '',   
    policyHolderName: '',   
    projectName: '',   
    projectId: '',   
    inviteCode: '',   
    applyNo: '',   
    applyTime: '',   
    policyLinkmanName: '',   
    policyLinkmanMobilename: '',   
    rate: undefined,
    policyUcc: '',   
    policyDays: '',   
    kaiBiaoTime: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    channelProvider: [{ required: true, message: '请输入渠道商!'},]
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
